#include "pelicanows.h"
#include <QDebug>
#include <QStringList>
#include "cfgfile.h"
#include <string.h>

PelicanoWS::PelicanoWS()
{
    CFGFile cfg;
    QString protocol("http://");
    QString endpoint = protocol+cfg.getServerIP()+":"+QString::number(cfg.getServerPort())+cfg.getServerPath()+"index.php?r=wsSettings/wsdl&ws=1";
    _endpoint = new char[endpoint.size()+1];
    strcpy(_endpoint,endpoint.toStdString().c_str());
}
PelicanoWS::~PelicanoWS()
{
    if(_endpoint!=NULL)
    {
        delete _endpoint;
        _endpoint = NULL;
    }
}

bool PelicanoWS::heartBeat()
{
    qDebug() << "heartBeat()";
    WsSettingsControllerBinding wss;
    wss.endpoint = _endpoint;
    qDebug() << "wss.endpoint: "<<wss.endpoint;
    bool response;

    int result = wss.ns5__HeartBeat(response);
    if(result==SOAP_OK)
    {
        qDebug() << "heartBeat() SOAP OK";
    }
    else
    {
        qDebug() << "heartBeat() SOAP Error";
    }
    return response;
}

SettingsResponse PelicanoWS::getSettings()
{
    qDebug() << "getSettings()";
    WsSettingsControllerBinding wss;
    wss.endpoint = _endpoint;
    qDebug() << "wss.endpoint: "<<wss.endpoint;

    SettingsResponse response;
    ns5__getSettingsResponse responseWS;

    int result = wss.ns5__getSettings(responseWS);
    if(result==SOAP_OK)
    {
        qDebug() << "SOAP OK";
        response.driveLetter = responseWS.return_->drive_USCOREletter.c_str();
        response.final_folder_ripping = responseWS.return_->final_USCOREfolder_USCOREripping.c_str();
        response.temp_folder_ripping = responseWS.return_->temp_USCOREfolder_USCOREripping.c_str();
        response.mymovies_password = responseWS.return_->mymovies_USCOREpassword.c_str();
        response.mymovies_username = responseWS.return_->mymovies_USCOREusername.c_str();
        response.id_device = responseWS.return_->id_USCOREdevice.c_str();
        //QString aux = responseWS.return_->time_USCOREfrom_USCOREreboot.c_str();
//        QStringList auxList = aux.split(":");
//        if(auxList.size()>=3)
//        {
//            response.time_from_reboot = QTime(auxList[0].toInt(),auxList[1].toInt(),auxList[2].toInt());
//        }
        response.time_from_reboot = QTime::fromString(responseWS.return_->time_USCOREfrom_USCOREreboot.c_str(),"hh:mm:ss");
//        aux = responseWS.return_->time_USCOREto_USCOREreboot.c_str();
//        auxList = aux.split(":");
//        if(auxList.size()>=3)
//        {
//            response.time_to_reboot = QTime(auxList[0].toInt(),auxList[1].toInt(),auxList[2].toInt());
//        }
        response.time_to_reboot = QTime::fromString(responseWS.return_->time_USCOREto_USCOREreboot.c_str(),"hh:mm:ss");

    }
    else
    {
        qDebug() << "SOAP Error";
        soap_print_fault(wss.soap, stderr);
    }
    return response;
}

void PelicanoWS::setInstalledAnydvdVersion(QString version)
{
    qDebug() << "setInstalledAnydvdVersion(QString version)";
    WsSettingsControllerBinding wss;
    wss.endpoint = _endpoint;
    bool response;
    int result = wss.ns5__setInstalledAnydvdVersion(version.toStdString(),response);

    if(result==SOAP_OK)
    {
        qDebug() << "SOAP OK";
    }
    else
    {
        qDebug() << "SOAP Error";
        soap_print_fault(wss.soap, stderr);
    }

}

AnydvdUpdateResponse PelicanoWS::checkForAnydvdUpdates()
{
    qDebug() << "PelicanoWS::checkForAnydvdUpdates()";
    AnydvdUpdateResponse response;
    WsSettingsControllerBinding wss;
    wss.endpoint = _endpoint;
    qDebug() << "EndPoint: "<<wss.endpoint;
    ns5__checkForAnydvdUpdatesResponse responseWs;
    int result = wss.ns5__checkForAnydvdUpdates(responseWs);

    if(result==SOAP_OK)
    {
        qDebug() << "SOAP OK";
        response.file_name = QString::fromStdString(responseWs.return_->file_USCOREname);
        response.url = QString::fromStdString(responseWs.return_->url);
        response.version = QString::fromStdString(responseWs.return_->version);
        response.action = (AnydvdUpdateResponse::eAnydvdActions)responseWs.return_->action;
        qDebug() << "response.file_name: "<<response.file_name;
        qDebug() << "response.url: "<<response.url;
        qDebug() << "response.version: "<<response.version;
        qDebug() << "response.action: "<<response.action;
    }
    else
    {
        qDebug() << "SOAP Error";
        soap_print_fault(wss.soap, stderr);
    }
    return response;
}

